local Lua__daolun = fk.CreateSkill {
  name = "Lua__daolun",
  anim_type = "support",
}

Fk:loadTranslationTable {
  ["Lua__daolun"] = "道论",
  [":Lua__daolun"] = "当有装备牌进入弃牌堆时，你可以弃一张牌并将此装备牌置入你的装备栏",
  ["#Lua__daolun-acquire"] = "道论：你可以弃置一张牌，将一张进入弃牌堆的装备牌置入你的装备栏",
  ["#Lua__daolun-acquire2"] = "道论：请选择装备牌置入你的装备栏",
}


-- 监听装备牌进入弃牌堆
Lua__daolun:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(Lua__daolun.name) or player:isKongcheng() then return false end

    -- 检查是否有装备牌因弃置进入弃牌堆
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if card.type == Card.TypeEquip then
            return true
          end
        end
      end
    end
  end,

  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      cancelable = true,
      pattern = ".",
      prompt = "#Lua__daolun-acquire",
      skill_name = Lua__daolun.name,
    })

    if #cards > 0 then
      event:setCostData(self, { cards = cards, moves = data })
      return true
    end
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)

    -- 获取装备牌
    local equips = {}
    for _, move in ipairs(cost_data.moves) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if card.type == Card.TypeEquip then
            table.insert(equips, info.cardId)
          end
        end
      end
    end

    if #equips > 0 then
      local selected_cards = room:askToChooseCardsAndChoice(player, {
        cards = equips,
        choices = { "OK" },
        skill_name = Lua__daolun.name,
        prompt = "#Lua__daolun-acquire2",
        cancelable = true,
      })
      if #selected_cards > 0 then
        room:moveCardIntoEquip(player, selected_cards, Lua__daolun.name, true, player)
      end
    end
  end,
})


return Lua__daolun
